﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<head>
    <meta charset="utf-8" />
</head>
<body>
    <div>可打开浏览器控制台查看结果</div>
    <script src="http://shared.ydstatic.com/js/jquery/jquery-3.1.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/emn178/js-sha256/build/sha256.min.js"></script>
    <script type="text/javascript">
        function getInput(input) {
            if (input.length == 0) {
                return null;
            }
            var result;
            var len = input.length;
            if (len <= 20) {
                result = input;
            } else {
                var startStr = input.substring(0, 10);
                var endStr = input.substring(len - 10, len);
                result = startStr + len + endStr;
            }
            return result;

        }
        var appKey = '280ef3eb4df1a70c';
        var key = 'BkswaaamcLT6kkDf8983KUUMmGbXv6Ug';//注意：暴露appSecret，有被盗用造成损失的风险
        var salt = new Date().getTime();
        var curtime = Math.round(new Date().getTime() / 1000);

        var query = 'good';
        // 多个query可以用\n连接  如 query='apple\norange\nbanana\npear'
        var from = 'auto';
        var to = 'zh-CHS';
        var str1 = appKey + getInput(query) + salt + curtime + key;
        var sign = sha256(str1);
        console.log(str1);
        $.ajax({
            url: 'http://openapi.youdao.com/api',
            type: 'post',
            dataType: 'jsonp',
            data: {
                q: query,
                appKey: appKey,
                salt: salt,
                from: from,
                to: to,
                curtime: curtime,
                sign: sign,
                signType: "v3"
            },
            success: function (data) {
                console.log(data);
            }
        });

    </script>
</body>
</html>
